﻿using System;
using System.Collections;
using System.Windows.Forms;
using System.Data.OracleClient;
using TimeTableEasy.src.Database;
using TimeTableEasy.src.Core;

namespace TimeTableEasy
{
    public partial class Cursus_Edit : Form
    {
        private int CampusID = 0;
        private ArrayList _IdList = new ArrayList();
        private OpCodes.TimeTableEasyAction _Action;

        public Cursus_Edit(OpCodes.TimeTableEasyAction action)
        {
            InitializeComponent();

            this._Action = action;
            _IdList.Clear();

            switch (action)
            {
                case OpCodes.TimeTableEasyAction.ACTION_ADD:
                    this.Text = "Ajouter un cursus";
                    editLbl.Text = "Ajouter un cursus";
                    addUserCampus.Text = "Ajouter";

                    OracleDataReader array = OracleConnector.Query("SELECT CAM_NUM, CAM_NOM FROM CAMPUS ORDER BY (CAM_NOM)");

                    if (array == null) return;

                    while (array.Read())
                    {
                        _IdList.Add((int)array.GetDecimal(0));
                        campusList.Items.Add(array.GetString(1));
                    }
                    break;
                case OpCodes.TimeTableEasyAction.ACTION_DEL:
                    this.Text = "Supprimer un cursus";
                    editLbl.Text = "Supprimer un cursus";
                    addUserCampus.Text = "Supprimer";
                    break;
                case OpCodes.TimeTableEasyAction.ACTION_MOD:
                    this.Text = "Modifier un cursus";
                    editLbl.Text = "Modifier un cursus";
                    addUserCampus.Text = "Modifier";
                    break;
            }
        }

        private void campusList_SelectedIndexChanged(object sender, EventArgs e)
        {
            CampusID = (int)_IdList[int.Parse(campusList.SelectedIndex.ToString())];
        }

        private void addUserCampus_Click(object sender, EventArgs e)
        {
            if (_Action == OpCodes.TimeTableEasyAction.ACTION_ADD)
            {
                if (nameTxtBx.Text != "")
                {
                    OracleDataReader array = OracleConnector.Query("INSERT INTO CURSUS VALUES ('" + nameTxtBx.Text + "')");
                    OracleDataReader array2 = OracleConnector.Query("INSERT INTO CAMPUS_CURSUS VALUES(" + CampusID + ", '" + nameTxtBx.Text + "')");
                    if ((array == null) || (array2 == null))
                        Label_Error.Text = "";
                    else
                        Label_Error.Text = "";
                }
                else
                    Label_Error.Text = "";
            }
        }
    }
}
